from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import WebDriver
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from config.config import WAIT_TIME


class BasePage:
    def __init__(self, driver: WebDriver):
        self.driver = driver
        self.wait = WebDriverWait(self.driver, WAIT_TIME)

    def find_element(self, locator):
        """查找单个元素"""
        return self.wait.until(EC.presence_of_element_located(locator))

    def find_elements(self, locator):
        """查找多个元素"""
        return self.wait.until(EC.presence_of_all_elements_located(locator))

    def click(self, locator):
        """点击元素"""
        element = self.find_element(locator)
        element.click()

    def send_keys(self, locator, text):
        """输入文本"""
        element = self.find_element(locator)
        element.clear()
        element.send_keys(text)

    def get_text(self, locator):
        """获取元素文本"""
        element = self.find_element(locator)
        return element.text

    def is_displayed(self, locator):
        """检查元素是否可见"""
        try:
            element = self.find_element(locator)
            return element.is_displayed()
        except:
            return False

    def swipe_up(self, duration=1000):
        """向上滑动"""
        size = self.driver.get_window_size()
        start_x = size['width'] * 0.5
        start_y = size['height'] * 0.8
        end_x = size['width'] * 0.5
        end_y = size['height'] * 0.2
        self.driver.swipe(start_x, start_y, end_x, end_y, duration)

    def swipe_down(self, duration=1000):
        """向下滑动"""
        size = self.driver.get_window_size()
        start_x = size['width'] * 0.5
        start_y = size['height'] * 0.2
        end_x = size['width'] * 0.5
        end_y = size['height'] * 0.8
        self.driver.swipe(start_x, start_y, end_x, end_y, duration)

    def swipe_left(self, duration=1000):
        """向左滑动"""
        size = self.driver.get_window_size()
        start_x = size['width'] * 0.8
        start_y = size['height'] * 0.5
        end_x = size['width'] * 0.2
        end_y = size['height'] * 0.5
        self.driver.swipe(start_x, start_y, end_x, end_y, duration)

    def swipe_right(self, duration=1000):
        """向右滑动"""
        size = self.driver.get_window_size()
        start_x = size['width'] * 0.2
        start_y = size['height'] * 0.5
        end_x = size['width'] * 0.8
        end_y = size['height'] * 0.5
        self.driver.swipe(start_x, start_y, end_x, end_y, duration)

    def swipe_to_element(self, element, max_swipes=10, direction=None):
        """滑动到指定元素"""
        for _ in range(max_swipes):
            if element.is_displayed():
                return True

            if direction == 'up':
                self.swipe_up()
            elif direction == 'down':
                self.swipe_down()
            elif direction == 'left':
                self.swipe_left()
            elif direction == 'right':
                self.swipe_right()

        return False

    def swipe_element_left(self, element, ratio=4, duration=500):
        """
        在指定元素上执行「左滑」手势
        :param element: 已定位的 WebElement
        :param ratio:   滑动距离占元素宽度的比例，默认 60%
        :param duration: 滑动持续时间，毫秒
        """
        rect = element.rect
        elem_w, elem_h = rect['width'], rect['height']
        center_y = rect['y'] + elem_h // 2

        start_x = rect['x'] + int(elem_w * (1 - ratio / 2))  # 右侧起点
        end_x = rect['x'] + int(elem_w * ratio / 2)  # 左侧终点

        TouchAction(self.driver) \
            .press(x=start_x, y=center_y) \
            .wait(ms=duration) \
            .move_to(x=end_x, y=center_y) \
            .release().perform()